package com.rongda.deploy.operation.deploy.service.impl;

import com.rongda.deploy.operation.deploy.dao.DAppDao;
import com.rongda.deploy.operation.deploy.pojo.App;
import com.rongda.deploy.operation.deploy.service.DAppService;
import com.rongda.deploy.operation.deploy.service.DServerService;
import com.rongda.deploy.operation.deploy.service.DeployService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (App)表服务实现类
 *
 * @author rongda
 * @since 2020-10-26 11:20:59
 */
@Service("dAppService")
public class DAppServiceImpl implements DAppService {
    @Resource
    private DAppDao dAppDao;

    @Resource
    private DeployService deployService;

    @Resource
    private DServerService dServerService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public App queryById(Long id) {
        return this.dAppDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<App> queryAllByLimit(int offset, int limit) {
        return this.dAppDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param dApp 实例对象
     * @return 实例对象
     */
    @Override
    public App insert(App dApp) {
        this.dAppDao.insert(dApp);
        return dApp;
    }

    /**
     * 修改数据
     *
     * @param dApp 实例对象
     * @return 实例对象
     */
    @Override
    public App update(App dApp) {
        this.dAppDao.update(dApp);
        return this.queryById(dApp.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.dAppDao.deleteById(id) > 0;
    }

    @Override
    public List<App> queryAllByServerId(Long serverId) {
        App app = new App();
        app.setServerId(serverId);
        List<App> list = this.dAppDao.queryAll(app);
        list.forEach(app1 -> {
            app1.setStatus(this.checkAppStatus(app1));
        });
        return list;
    }

    @Override
    public boolean reloadApp(Long id) {
        App app = this.dAppDao.queryById(id);
        StringBuffer stringBuffer = new StringBuffer();
        StringBuffer commend = stringBuffer.append("cd ").
                append(app.getDeployPath()).append("\n").
                append("./start.sh").append(" ").
                append(app.getStartScript());
        System.out.println("commend命令" + commend);
        try {
            this.deployService.executeCommend(commend.toString(), dServerService.queryById(app.getServerId()));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public Integer checkAppStatus(App app) {
        StringBuffer sb = new StringBuffer();
        StringBuffer commend = sb.append("ps -ef|grep ").append(app.getStartScript());
        String result = "";
        if (app.getStartScript().contains("jar")) {
            result = this.deployService.executeCommend(commend.toString(), dServerService.queryById(app.getServerId()));
            if (result.contains("-Dspring.profiles.active=prod")) {
                return 1;
            } else {
                return 0;
            }
        } else {
            result = this.deployService.executeCommend("ps -ef|grep tomcat", dServerService.queryById(app.getServerId()));
            System.out.println(result);
            if (result.contains("/usr/local/java/jdk1.8.0_11/bin/java")) {
                return 1;
            } else {
                return 0;
            }
        }
    }
}